package chen.web.controller.interceptor;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import chen.web.MyContextProvider;
import chen.web.controller.blog.ArticleController;
import chen.web.controller.forum.model.Config;
import chen.web.controller.forum.model.Model;
import chen.web.controller.forum.model.URL;
import chen.web.controller.forum.model.Utils;

public class TemplateInterceptor extends HandlerInterceptorAdapter {
	
	@Autowired
	private URL url;
	@Autowired
	private Config config ;
	@Autowired
	private Utils utils ;
	
	@Override
	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		// TODO Auto-generated method stub
		if(modelAndView == null){
			return;
		}
		String viewName = modelAndView.getViewName();
		if(viewName == null || viewName.startsWith("redirect:")){
			return;
		}
		if(handler instanceof ArticleController){
			String theme = MyContextProvider.getMyContext().getTheme();
			if(theme == null)	theme = "default";
			modelAndView.setViewName(theme + "/" + viewName);
		} else {
			modelAndView.setViewName(viewName);	
		}
		modelAndView.addObject("url", url);
		modelAndView.addObject("config", config);
		modelAndView.addObject("utils", utils);
		//modelAndView.addObject("model", model);
		super.postHandle(request, response, handler, modelAndView);
	}

}
